/**
 * EnemyManager
 * Manage Enemy entities
 **/
var EnemyManager = function() {
	this.entities = [];
	this.popTime = null;
	this.entitiesName = [];
	this.russianBullet = null;

	this.init = function() {
		for (x in EnemyConf) {
			this.entitiesName.push(x);
		}

		this.createOne();
		this.popTime = +new Date();
		this.russianBullet = 0.1;
	}

	this.destroy = function() {
		for(var iE = 0, c = this.entities.length; iE < c; iE++) {
			this.entities.remove(iE);
			c--;
		}
		this.popTime = null;
	}

	this.update = function() {
		this.garbage();
		if (interval(this.popTime, rand(1, game.mLevel.level.conf.popTime))) {
			this.createOne();
			this.popTime = +new Date();
		}

		for(var iE = 0, c = this.entities.length; iE < c; iE++) {
			this.entities[iE].update();
		}
	}

	this.render = function() {
		for(var iE = 0, c = this.entities.length; iE < c; iE++) {
			this.entities[iE].render();
		}
	}

	this.createOne = function() {
		// Create a new enemy instance
		var enemy = new Enemy();
		// Initialize it
		enemy.init(this.entitiesName.pickup());
		// Add the instance to the entities array
		this.entities.push(enemy);
	}

	this.deleteOne = function(instance) {
		// Delete an instance of enemy
		for(var iE = 0, c = this.entities.length; iE < c; iE++) {
			if(this.entities[iE] === instance) {
				this.entities.remove(iE);
				c--;
			}
		}
	}

	this.garbage = function() {
		for(var iE = 0, cE = this.entities.length; iE < cE; iE++) {
			if(this.entities[iE].toGarbage == true) {
				this.entities.splice(iE, 1);
				cE--;
			}
		}
	}

};